package com.example.onlinemonitoring.serviceImpl;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Wxblqsensor;
import com.example.onlinemonitoring.model.Wxblqhistory;
import org.springframework.stereotype.Service;
import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.service.WxblqsensorService;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.Util.HexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.example.onlinemonitoring.dao.WxblqhistoryMapper;
import com.example.onlinemonitoring.dao.WxblqsensorMapper;
import tk.mybatis.mapper.entity.Example;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.ptconfig;
import org.apache.ibatis.session.RowBounds;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
* @description Wxblqsensor接口实现
* @author  generator
* @since  2022-11-14 09:22:58
*/
@Service("wxblqServiceImpl")
public class WxblqsensorServiceImpl extends SensorAbstractBaseService<Wxblqsensor, Wxblqhistory> implements WxblqsensorService {
     //这里需要重写getDt方法,用来处理图表数据
    //public String[] getDt(List<Wxblqhistory> list,String[] dt,chartres cts,float[][] sres,SimpleDateFormat sdf,SimpleDateFormat csdf,SimpleDateFormat sdf2){return null;}
    @Autowired
    private WxblqsensorMapper wxblqsensormapper;
    @Autowired
    private WxblqhistoryMapper wxblqhistorymapper;
    public void UpdateStatus(Appdevice ad, Integer addrindex, int i, String string) {
        Wxblqsensor record = new Wxblqsensor();
        record.setStationid(ad.getStationid());
        record.setDevicegroup(ad.getDevicegroup());
        switch (string) {
            case "blq":
            record.setLastcommunicationstatusBlq(i);
                break;
            case "pt":
            record.setLastcommunicationstatusPt(i);
                break;
            case "wsd":
            record.setLastcommunicationstatusWsd(i);
                break;
            default:
                break;
        }
        if(i != 0){
            record.setLastalarmlevel(-1);
        }
        record.setDeviceaddr(addrindex);
        Update(record, false);
    }
    public List<Wxblqhistory> getRecent6Data(Wxblqhistory pthis) {
        Example example = new Example(Wxblqhistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", pthis.getStationid());
        criteria.andEqualTo("devicegroup", pthis.getDevicegroup());
        criteria.andEqualTo("deviceaddr", pthis.getDeviceaddr());
        criteria.andEqualTo("ptaddr", pthis.getPtaddr());
        example.orderBy("datatime").desc();
        RowBounds rb = new RowBounds(0,6);
        List<Wxblqhistory> blqlist = wxblqhistorymapper.selectByExampleAndRowBounds(example, rb);
        return blqlist;
    }
    public MyResult<ptconfig> SelectPt(Wxblqsensor record) {
        MyResult<ptconfig> result = new MyResult<ptconfig>();
        List<ptconfig> list = wxblqsensormapper.selectpt();
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list!=null&&!list.isEmpty()?list.size():0);
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }
    @Transactional
    public MyResult<Integer> UpdatePt(ptconfig ptconfig, Integer stationid, Integer devicegroup) {
        Example example = new Example(Wxblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        wxblqsensormapper.setPtaddr(stationid,devicegroup,ptconfig.getPtaddr());
        if (ptconfig.getBlqmin()!= 0 && ptconfig.getBlqmax()!= 0) {
            Wxblqsensor record = new Wxblqsensor();
            record.setPtaddr(ptconfig.getPtaddr());
            criteria.andGreaterThanOrEqualTo("deviceaddr", ptconfig.getBlqmin());
            criteria.andLessThanOrEqualTo("deviceaddr", ptconfig.getBlqmax());
            updateByExampleSelective(record, example);
        }
        return new MyResult<Integer>(true);
    }

    public List<Wxblqsensor> getPtModel(Wxblqsensor alarm) {
        Example example = new Example(Wxblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", alarm.getStationid());
        criteria.andEqualTo("devicegroup", alarm.getDevicegroup());
        criteria.andEqualTo("ptaddr", alarm.getPtaddr());
        return wxblqsensormapper.selectByExample(example);
    }
}
